import 'package:user_data_model/data/user_data.dart';
import 'package:rxdart/rxdart.dart';
import 'package:user_data_model/model/user_model.dart';
import 'package:user_data_model/user_register_item.dart';

/**
 *
 * @ProjectName:    user_data_model
 * @ClassName:      user_bloc
 * @Description:    dart类作用描述
 * @Author:         孙浩
 * @QQ:             243280864
 * @CreateDate:     2021/1/28 11:36 PM
 */
class UserBloc {
  String _token;

  UserData _userData;

  PublishSubject<UserData> _userSub = PublishSubject();

  getUserInfo() async {
    _userData = await UserModel(UserRegister.instance.baseDio)
        .getUserInfoFromNet()
        .catchError((e) {
      _userData = null;
    });
    _userSub.add(_userData);
  }

  removeUserData() {
    _userData = null;
    _userSub.add(_userData);
  }

  void unRegisterUser() {}

  void bindToken(String token) {
    _token = token;
  }

  get userStream => _userSub.stream;

  // 工厂模式
  factory UserBloc() => _getInstance();

  static UserBloc get instance => _getInstance();
  static UserBloc _instance;

  UserBloc._internal() {
    // 初始化
  }

  static UserBloc _getInstance() {
    if (_instance == null) {
      _instance = new UserBloc._internal();
    }
    return _instance;
  }
}
